from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import os
import wget
chrome_driver_path = "資料夾路徑/chromedriver"
service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=service)
driver.get("https://pic.sogou.com")
search = driver.find_element(By.NAME, "query")
search.send_keys("企鵝")
search.send_keys(Keys.RETURN)
imgs = driver.find_elements(By.TAG_NAME, "img")
keyword = "penguin"
path = os.path.join(keyword)
os.mkdir(path) #創建資料夾
count = 0
for img in imgs:
save_as = os.path.join(path, keyword + str(count) + ".jpg")
src = img.get_attribute("src")
print(img.get_attribute("src"))
if src:
wget.download(src, save_as)
count += 1
以上是最基本的爬蟲,如果要下載更多圖片,需要模擬滾動視窗來瀏覽更多圖片。